'use strict';

// const logger = require('koa-logger')
// const log4js = require('koa-log4js')
// const session = require('koa-generic-session')
const config = require('../config');
const morgan = require('koa-morgan');
const bodyparser = require('koa-bodyparser');
// const cors = require('koa-cors')
const router = require('../router');
const logger = require('../lib/logger').logger;
const format = config.app.isProd ? 'combined' : 'dev';
const options = {
  stream: {
    write (str) {
      logger.info(str);
    }
  }
};

module.exports = function (app) {
  app.keys = ['key', 'keys'];
  // app.use(log4js())
  // app.use(logger())
  // 跨域请求
  // app.use(cors({origin: '*'}))
  app.use(morgan.middleware(format, options));
  // app.use(session())
  app.use(bodyparser());
  app.use(router.routes());
  // 记录错误的日志，原 koa 会自动 catch 所有日志
  app.on('error', (err, ctx) => {
    logger.error('server error', err);
  });
};
